package com.team11.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.team11.server.config.SplicingConfig;
import com.team11.server.mapper.CFilesMapper;
import com.team11.server.mapper.HInformationMapper;
import com.team11.server.pojo.CFiles;
import com.team11.server.pojo.HInformation;
import com.team11.server.pojo.Homework;
import com.team11.server.mapper.HomeworkMapper;
import com.team11.server.pojo.dto.CheckWork;
import com.team11.server.pojo.dto.RespBean;
import com.team11.server.pojo.dto.StudentAnswer;
import com.team11.server.service.IHomeworkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.team11.server.util.file.GetFilesUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author pjc
 * @since 2021-06-17
 */
@Service
public class HomeworkServiceImpl extends ServiceImpl<HomeworkMapper, Homework> implements IHomeworkService {

    @Resource
    private HomeworkMapper homeworkMapper;

    @Resource
    private CFilesMapper cFilesMapper;

    @Resource
    private HInformationMapper hInformationMapper;

    /**
     * 获取学生答题列表
     *
     * @param homeworkId s
     * @return s
     */
    @Override
    public StudentAnswer getStudentAnswer(Integer homeworkId) {
        StudentAnswer studentAnswer = new StudentAnswer();
        try {
            Homework homework = homeworkMapper.selectOne(new QueryWrapper<Homework>().eq("id", homeworkId).eq("status", 1));
            if (homework != null) {
                studentAnswer.setAnswer(homework.getAnswer());
                if (homework.getCFId() != null) {
                    CFiles cFiles = cFilesMapper.selectOne(new QueryWrapper<CFiles>().eq("c_f_id", homework.getCFId()));
                    if (cFiles != null) {
                        studentAnswer.setCFile(cFiles.getCFile());
                        studentAnswer.setCFName(cFiles.getCFName());
                        studentAnswer.setCFId(cFiles.getCFId());
                    }
                }
            }
            return studentAnswer;
        } catch (Exception e) {
            e.printStackTrace();
        }
       return null;

    }

    /**
     * 获取老师检查作业列表(评完分的)
     *
     * @param homeworkId s
     * @return s
     */
    @Override
    public CheckWork getCheckWork(Integer homeworkId) {
        try {
            if (homeworkMapper.selectOne(new QueryWrapper<Homework>().eq("id", homeworkId)).getStatus() == 2) {
                CheckWork checkWork = new CheckWork();
                checkWork.setHomeworkId(homeworkId);
                Homework homework = homeworkMapper.selectOne(new QueryWrapper<Homework>().eq("id", homeworkId).eq("status", 2));
                checkWork.setReply(homework.getReply());
                checkWork.setScore(homework.getScore());
                checkWork.setStatus(homework.getStatus());
                if (homework.getReplyCfd() != null) {
                    CFiles cFiles = cFilesMapper.selectOne(new QueryWrapper<CFiles>().eq("c_f_id", homework.getReplyCfd()));
                    if (cFiles != null) {
                        checkWork.setCFId(cFiles.getCFId());
                        checkWork.setCFile(cFiles.getCFile());
                        checkWork.setCFName(cFiles.getCFName());
                    }
                }
                return checkWork;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 老师检查作业（还没评分）
     *
     * @param checkWork s
     * @param files s
     * @param request s
     * @return  s
     */
    @Override
    public RespBean CheckWork(CheckWork checkWork, MultipartFile[] files, HttpServletRequest request) {
        try {
            if (checkWork.getScore() == null) {
                return RespBean.error("分数不能为空!");
            }
            if (homeworkMapper.selectOne(new QueryWrapper<Homework>()
                    .eq("id", checkWork.getHomeworkId())).getStatus() == 1) {
                //附件
                CFiles cFiles1 = new CFiles();
                Homework homework1 = new Homework();
                List<String> list1 = new ArrayList<>();
                List<String> list2 = new ArrayList<>();
                if (files != null) {
                    List<String> list = GetFilesUtil.getFiles(files, request);
                    list1.add(list.get(0));
                    list2.add(list.get(1));
                    cFiles1.setCFName(SplicingConfig.Splicing(list1));
                    cFiles1.setCFile(SplicingConfig.Splicing(list2));
                    cFiles1.setCFType(2);
                    cFiles1.setHomeworkId(checkWork.getHomeworkId());
                    cFiles1.setHId(homeworkMapper.selectOne(new QueryWrapper<Homework>().eq("id", checkWork.getHomeworkId())).getHId());
                    cFiles1.setTId(hInformationMapper.selectOne(new QueryWrapper<HInformation>().eq("h_id", cFiles1.getHId())).getTId());
                    cFilesMapper.insert(cFiles1);
                    homework1.setReplyCfd(cFiles1.getCFId());
                }
                homework1.setScore(checkWork.getScore());
                homework1.setId(checkWork.getHomeworkId());
                homework1.setStatus(2);
                homework1.setReply(checkWork.getReply());

                if (homeworkMapper.updateById(homework1) == 1) {
                    return RespBean.success("检查成功!");
                }
            }else {
                return RespBean.error("这份作业已经检查过了哦！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RespBean.error("检查失败!");
    }
}

